// This source is subject to the Microsoft Public License.
// See http://www.microsoft.com/resources/sharedsource/licensingbasics/sharedsourcelicenses.mspx.
// All other rights reserved.

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Xml;

namespace QuoteAdvantageEditor
{
	/// <summary>
	/// Edit an item in the category tree for parent products
	/// </summary>
	public class CategoryModifyDlg : System.Windows.Forms.Form
	{
      private AuthoringEngine eng = null;
      protected XmlNode objCategoryNode = null;
      private System.Windows.Forms.Button btnOK;
      private System.Windows.Forms.Button btnCancel;
      private System.Windows.Forms.Label label1;
      private System.Windows.Forms.Label label2;
      private System.Windows.Forms.TextBox txtCategoryName;
      private System.Windows.Forms.TextBox txtCategoryId;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

      public AuthoringEngine Engine
      {
         get { return eng; }
         set { eng = value; }
      }
      
      public XmlNode CategoryNode
      {
         get { return objCategoryNode; }
         set { objCategoryNode = value; }
      }
      
      public CategoryModifyDlg()
		{
			//
			// Required for Windows Form Designer support
			//
			InitializeComponent();

			//
			// TODO: Add any constructor code after InitializeComponent call
			//
		}

		/// <summary>
		/// Clean up any resources being used.
		/// </summary>
		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
         this.btnOK = new System.Windows.Forms.Button();
         this.btnCancel = new System.Windows.Forms.Button();
         this.label1 = new System.Windows.Forms.Label();
         this.txtCategoryName = new System.Windows.Forms.TextBox();
         this.label2 = new System.Windows.Forms.Label();
         this.txtCategoryId = new System.Windows.Forms.TextBox();
         this.SuspendLayout();
         // 
         // btnOK
         // 
         this.btnOK.Location = new System.Drawing.Point(112, 128);
         this.btnOK.Name = "btnOK";
         this.btnOK.TabIndex = 4;
         this.btnOK.Text = "OK";
         this.btnOK.Click += new System.EventHandler(this.btnOK_Click);
         // 
         // btnCancel
         // 
         this.btnCancel.DialogResult = System.Windows.Forms.DialogResult.Cancel;
         this.btnCancel.Location = new System.Drawing.Point(192, 128);
         this.btnCancel.Name = "btnCancel";
         this.btnCancel.TabIndex = 5;
         this.btnCancel.Text = "Cancel";
         this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
         // 
         // label1
         // 
         this.label1.Location = new System.Drawing.Point(16, 16);
         this.label1.Name = "label1";
         this.label1.Size = new System.Drawing.Size(104, 16);
         this.label1.TabIndex = 0;
         this.label1.Text = "Category Name:";
         // 
         // txtCategoryName
         // 
         this.txtCategoryName.Location = new System.Drawing.Point(16, 32);
         this.txtCategoryName.Name = "txtCategoryName";
         this.txtCategoryName.Size = new System.Drawing.Size(248, 21);
         this.txtCategoryName.TabIndex = 1;
         this.txtCategoryName.Text = "";
         // 
         // label2
         // 
         this.label2.Location = new System.Drawing.Point(16, 72);
         this.label2.Name = "label2";
         this.label2.Size = new System.Drawing.Size(104, 16);
         this.label2.TabIndex = 2;
         this.label2.Text = "Category ID:";
         // 
         // txtCategoryId
         // 
         this.txtCategoryId.Location = new System.Drawing.Point(16, 88);
         this.txtCategoryId.Name = "txtCategoryId";
         this.txtCategoryId.Size = new System.Drawing.Size(248, 21);
         this.txtCategoryId.TabIndex = 3;
         this.txtCategoryId.Text = "";
         // 
         // CategoryModifyDlg
         // 
         this.AcceptButton = this.btnOK;
         this.AutoScaleBaseSize = new System.Drawing.Size(5, 14);
         this.CancelButton = this.btnCancel;
         this.ClientSize = new System.Drawing.Size(280, 160);
         this.Controls.Add(this.txtCategoryId);
         this.Controls.Add(this.label2);
         this.Controls.Add(this.txtCategoryName);
         this.Controls.Add(this.label1);
         this.Controls.Add(this.btnCancel);
         this.Controls.Add(this.btnOK);
         this.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((System.Byte)(0)));
         this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.FixedDialog;
         this.MaximizeBox = false;
         this.MinimizeBox = false;
         this.Name = "CategoryModifyDlg";
         this.ShowInTaskbar = false;
         this.StartPosition = System.Windows.Forms.FormStartPosition.CenterParent;
         this.Text = "Category";
         this.Load += new System.EventHandler(this.CategoryModifyDlg_Load);
         this.ResumeLayout(false);

      }
		#endregion

      private void btnOK_Click(object sender, System.EventArgs e)
      {
         if (txtCategoryName.Text == "")
         {
            MessageBox.Show(this, "Please enter a name for this category", "QuoteAdvantage Editor");
            txtCategoryName.Focus();
         }
         else if (txtCategoryId.Text == "")
         {
            MessageBox.Show(this, "Please enter an ID for this category", "QuoteAdvantage Editor");
            txtCategoryId.Focus();
         }
         else
         {
            int nCategoryCount = CountCategories(txtCategoryId.Text, objCategoryNode.OwnerDocument);

            if (nCategoryCount > 0)
            {
               MessageBox.Show(this, "A category with this ID already exists. Please specify a different Category ID", "QuoteAdvantage Editor");
               txtCategoryId.Focus();
            }
            else
            {
               objCategoryNode.Attributes["name"].Value = txtCategoryName.Text;
               objCategoryNode.Attributes["categoryId"].Value = txtCategoryId.Text;
               this.DialogResult = DialogResult.OK;
               this.Close();
            }
         }
      }

      private void btnCancel_Click(object sender, System.EventArgs e)
      {
         this.Close();      
      }

      private void CategoryModifyDlg_Load(object sender, System.EventArgs e)
      {
         txtCategoryName.Text = objCategoryNode.Attributes["name"].Value;
         txtCategoryId.Text = objCategoryNode.Attributes["categoryId"].Value;
      }


      // Check for existing categories with this Category ID
      private int CountCategories(string strCategoryId, XmlDocument objDocument)
      {
         XmlNode objParentNode = objDocument.SelectSingleNode("//CategoryList");
         string strTarget = strCategoryId.ToUpper();
         int nTotal = 0;

         foreach (XmlNode node in objParentNode.ChildNodes)
         {
            string strCat = node.Attributes["categoryId"].Value.ToUpper();

            if (strCat == strTarget)
            {
               // Don't count the node that we're currently editing
               if (node != objCategoryNode)
               {
                  nTotal++;
               }
            }
         }

         return nTotal;
      }

	} // end class
}
